﻿using System;
using SPPropertyBagManagement.Code;
using System.Drawing;

namespace SPPropertyBagManagement.Layouts.SPPropertyBagManagement
{
    public partial class Delete : PropertyManagementBasePage
    {
        /// <summary>
        /// Represents the method that handles the Init event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            SetPageTitle("Delete property: " + PropertyName + " | " + Scope);
            lblPropertyName.Text = PropertyName;
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected new void Page_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Shows the message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="isError">if set to <c>true</c> [is error].</param>
        protected void ShowMessage(string message, bool isError)
        {
            lblMessage.Text = message;

            if (isError)
            {
                lblMessage.ForeColor = Color.Red;
            }
            else
            {
                lblMessage.ForeColor = Color.Silver;
            }

            lblMessage.Visible = true;
        }

        /// <summary>
        /// Hides the message.
        /// </summary>
        protected void HideMessage()
        {
            lblMessage.Visible = false;
        }

        /// <summary>
        /// Handles the OnClick event of the btnConfirmDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnConfirmDelete_OnClick(object sender, EventArgs e)
        {
            DeleteProperty();
        }

        /// <summary>
        /// Deletes the property.
        /// </summary>
        private void DeleteProperty()
        {
            try
            {
                Property myProp = PropertyManager.GetProperty(Scope, ObjectId, ListItemObjectId, PropertyName);
                myProp.ToBeRemoved = true;
                myProp.PropertyValue = null;

                myProp.Update();

                CloseDialog(0, "deleteOk", "Delete successful.");
            }
            catch (Exception ex)
            {
                ShowMessage("Delete failed: " + ex.Message, true);
            }
        }
    }
}
